package com.zhiche.lisa.bms.controller.arfund;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.BMSApplication;
import com.zhiche.lisa.bms.pojo.vo.arFund.ArFundVO;
import com.zhiche.lisa.bms.service.arfund.ArFundService;
import com.zhiche.lisa.bms.service.utils.UaaUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BMSApplication.class)
public class ArFundControllerText {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext context;

    private String token;

    @Autowired
    private UaaUtil uaaUtil;

    @Autowired
    private Filter springSecurityFilterChain;
    @Autowired
    private ArFundService arFundService;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilter(springSecurityFilterChain).build();
//        token = uaaUtil.getTenantToken("15110288389");
        token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnbXRfY3JlYXRlIjoiMTUzNzg0NjY1NTM4NCIsImFjY291bnRJZCI6MTAyLCJ1c2VyX25hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIiwic2NvcGUiOlsiYWxsIl0sInJvbGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJ0ZW5hbnRJZCI6MSwiY29ycE5hbWUiOiLkuK3ogZTnianmtYHvvIjkuK3lm73vvInmnInpmZDlhazlj7giLCJleHAiOjE1Mzc4NTM4NTUsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiY2RiY2U0MzgtZDM2NS00ZGVkLTg5ZjgtNzFjOGMzNGM1MjJhIiwiY2xpZW50X2lkIjoiZGV2b3BzIiwidXNlcm5hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIn0.0GqaTsnvJirit_HSSy6nbaZw07pZldLYQt7LLnhOx4Q";
    }

    @Test
    public void queryArFundLine() throws Exception {
        Page<ArFundVO> page = new Page<>(1, 10);
        Map<String, Object> map = Maps.newHashMap();
//        map.put("lspName","路易通达");
//        map.put("fleetName","广东A队");
//        map.put("withholdWay","现金");
//        map.put("status","新增");
        map.put("gmtCreate", "2018-08-27");
        map.put("gmtCreateVo", "2018-09-30");
        page.setCondition(map);
        String a = JSONObject.toJSONString(page);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/arFund/queryArFundLine")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果状态：" + status);
        System.out.println(content);
        Assert.assertEquals(status, 200);
    }

    @Test
    public void deleteArFund() throws Exception {
        List<Long> list = new ArrayList<>();
        list.add(4L);
        list.add(5L);
        list.add(3L);
        String a = JSONObject.toJSONString(list);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/arFund/deleteArFund")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果状态：" + status);
        System.out.println(content);
        Assert.assertEquals(status, 200);
    }

    @Test
    public void examineArFund() throws Exception {
        List<Long> list = new ArrayList<>();
        list.add(14L);
        list.add(4L);
        list.add(6L);
        list.add(7L);
        list.add(8L);
        list.add(9L);
        list.add(10L);
        list.add(11L);
        list.add(12L);
        list.add(13L);
        list.add(15L);
        String a = JSONObject.toJSONString(list);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/arFund/examineArFund")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果状态：" + status);
        System.out.println(content);
        Assert.assertEquals(status, 200);
    }
}
